<?php
/**
 * Description of mydefault
 *
 * @author administrator
 */
class mydefault extends Action {
    
    public $dba;

    public function __construct() {
        parent::__construct();
        $this->dba=new db();
    }
    
    public function index(){
        $this->display("index.html");
    }
    
    public function yanzhengma(){
        buildImageVerify('4', '2', "png","48","22","yzmaaa");
    }
    
    public function channel(){
        $id = $_REQUEST['id'];
//        $sql = "select * from article where `channel`=\"$id\" order by `date` desc";
//        $article_data = $this->dba->fetch_all($sql);
        $this->assign("channel_id",$id);
        
        //当前显示的页码
        $page = $_REQUEST['page'] ? $_REQUEST['page'] : 1;   
        //设置每页显示多少条数据
        $page_num = 15;
        //根据设置的数据显示条数和当前页码设置sql查询条件
        $page_true = $page * $page_num - $page_num;
        //sql查询语句
        $sql = "select * from `article` where `channel`=\"$id\" order by `date` desc limit $page_true,$page_num";
        //数据查询结果
        $row=$this->dba->fetch_all($sql);
        //表中共有多少条数据
        $rownum_a=mysql_fetch_assoc($this->dba->query("select count(*) as total from `article` where `channel`=\"$id\""));
        $rownum = $rownum_a['total'];
        //分页：根据总共的数据条数分出共有多少页，// floor 舍去法取整，后面那部分表示如果能求模，则加1，否则为0
        $page_row = floor($rownum/$page_num) + ($rownum%$page_num ? 1 : 0); 
        //页码条的起始页，这里是为了避免起始页出现负数
        $page_ts = $page <= 2 ? 1 : $page - 2;  
        //页码条的终止页，当前显示页小于等于3时，终止页都设置为5
        $page_tw = $page <= 3 ? 5 : $page + 2;  
        //这里是为了避免终止页无限制增加
        $page_tw = $page_tw > $page_row ? $page_row : $page_tw;
        //上一页
        $page_prev = $page == 1 ? $page : $page - 1;  //上一页
        //下一页
        $page_next = $page == $page_row ? $page_row : $page + 1;  //下一页
        ////////////模板赋值（分页需要的变量）/////////////////////
        $this->assign("pageaa", range($page_ts, $page_tw));  //显示页码条,range 建立包含指定范围的数组
        $this->assign("page_row", $page_row);  //共有多少页
        $this->assign("page_prev", $page_prev); //上一页
        $this->assign("page_next", $page_next); //下一页
        $this->assign("page_true",$page_true);  //这个赋值是为了序号能连续显示，因为数据表中ID字段有可能不是连续的,不能直接显示
        ////////////模板赋值（数据表查询结果）/////////////////////
        $this->assign("article_data", $row);

        //////////////////
        
//        $this->assign("article_data", $article_data);
        $this->display("content_2.html");
    }
    
    public function readarticle(){
        $article_id = $_REQUEST['artid'];
        $sql = "select * from article where `id`=\"$article_id\"";
        $article_data = $this->dba->fetch_arrone($sql);
        $article_content=html_entity_decode($article_data['content'], ENT_QUOTES, "UTF-8");
        $this->assign("article_title", $article_data['title']);
        $this->assign("article_data", $article_content);
        $this->display("readarticle.html");
    }
    
    public function content(){
        $channelid=$_REQUEST['id'];
        $id;
        $boolaa = true;
        //判断点击的栏目是否有子栏目
        $temp = $this->dba->fetch_arrone("select count(*) as sum from channel where parent_id=\"$channelid\"");
        if($temp['sum'] == '0'){
            $temp = $this->dba->fetch_arrone("select * from channel where id=\"$channelid\"");
            if($temp['parent_id']!='0'){
                $displaychannelbool = true;
            }else{
                $displaychannelbool = false;
            }
        }else{
            $displaychannelbool = true;
        }
        
        //循环查询当前点击的栏目的子栏目,并返回他的ID
        while($boolaa){
            $sql = "select id,parent_id,content from channel where parent_id=\"$channelid\" order by sort limit 0,1";
            $data = $this->dba->fetch_arrone($sql);

            if(empty($data)){
                $sql = "select id,parent_id,content from channel where id=\"$channelid\"";
                $data = $this->dba->fetch_arrone($sql);
                if(empty($data)){
                    $boolaa=false;
                }else{
                    $channelid= $data['id'];
                    $id=$data['content'];
                    $boolaa=false;
                }
            }else{
                $channelid= $data['id'];
            }
        }
        //查询单页面数据并赋值模板
        $sql = "select * from SinglePage where id=\"$id\"";
        $data = $this->dba->fetch_arrone($sql);

        $content_data = html_entity_decode($data['content'], ENT_QUOTES, "UTF-8");
        $this->assign("content_data", $content_data);
        //根据是否有子栏目显示不同的模板文件
        if($displaychannelbool){
            $this->display("content.html");
        }else{
            $this->display("content_3.html");
        }
    }
    
    public function defaultfunc(){
        $this->display("default.html");
    }
}

?>
